"""
预约订单模型
"""
from sqlalchemy import Column, String, Float, Integer, DateTime, Boolean, Enum as SQLEnum
from sqlalchemy.sql import func
from app.core.database import Base
import enum


class BookingType(str, enum.Enum):
    """预约类型"""
    TEAM = "team"  # 球队包场
    MATCH = "match"  # 散客报名


class PaymentMethod(str, enum.Enum):
    """支付方式"""
    CAPTAIN = "captain"  # 队长全额
    AA = "aa"  # 队员分摊


class OrderStatus(str, enum.Enum):
    """订单状态"""
    PENDING = "pending"  # 待支付
    PAID = "paid"  # 已支付
    CONFIRMED = "confirmed"  # 已确认
    FINISHED = "finished"  # 已完成
    CANCELLED = "cancelled"  # 已取消


class Booking(Base):
    """预约订单表"""
    __tablename__ = "bookings"
    
    id = Column(String(36), primary_key=True, index=True)
    type = Column(SQLEnum(BookingType), nullable=False)
    user_id = Column(String(36), nullable=False, index=True)
    user_name = Column(String(100), nullable=False)
    
    # 场地信息
    venue_id = Column(String(36), nullable=False, index=True)
    venue_name = Column(String(200), nullable=False)
    venue_type = Column(String(10))
    latitude = Column(Float)
    longitude = Column(Float)
    address = Column(String(500))
    
    # 时间信息
    date = Column(String(10), nullable=False, index=True)  # YYYY-MM-DD
    start_time = Column(String(5), nullable=False)  # HH:mm
    end_time = Column(String(5), nullable=False)  # HH:mm
    
    # 费用信息
    venue_price = Column(Float, nullable=False)
    service_fee = Column(Float, default=0.0)
    total_price = Column(Float, nullable=False)
    payment_method = Column(SQLEnum(PaymentMethod))
    
    # 球队预约特有
    team_id = Column(String(36), index=True)
    team_name = Column(String(200))
    
    # 散客报名特有
    match_id = Column(String(36), index=True)
    position = Column(String(20))  # 位置
    
    # 订单状态
    status = Column(SQLEnum(OrderStatus), default=OrderStatus.PENDING, index=True)
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    paid_at = Column(DateTime(timezone=True))
    cancelled_at = Column(DateTime(timezone=True))
    cancel_reason = Column(String(500))
    
    def to_dict(self):
        """转换为字典"""
        return {
            "id": self.id,
            "type": self.type,
            "userId": self.user_id,
            "userName": self.user_name,
            "venueId": self.venue_id,
            "venueName": self.venue_name,
            "venueType": self.venue_type,
            "location": {
                "latitude": self.latitude,
                "longitude": self.longitude,
                "address": self.address
            },
            "date": self.date,
            "startTime": self.start_time,
            "endTime": self.end_time,
            "venuePrice": self.venue_price,
            "serviceFee": self.service_fee,
            "totalPrice": self.total_price,
            "paymentMethod": self.payment_method,
            "teamId": self.team_id,
            "teamName": self.team_name,
            "matchId": self.match_id,
            "position": self.position,
            "status": self.status,
            "createdAt": self.created_at.isoformat() if self.created_at else None,
            "paidAt": self.paid_at.isoformat() if self.paid_at else None,
            "cancelledAt": self.cancelled_at.isoformat() if self.cancelled_at else None,
            "cancelReason": self.cancel_reason,
            "participants": []  # 需要从 BookingParticipant 表查询
        }


class BookingParticipant(Base):
    """预约参与者表（球队预约时的队员）"""
    __tablename__ = "booking_participants"
    
    id = Column(String(36), primary_key=True, index=True)
    booking_id = Column(String(36), nullable=False, index=True)
    user_id = Column(String(36), nullable=False, index=True)
    user_name = Column(String(100), nullable=False)
    avatar = Column(String(500))
    position = Column(String(20))
    
    # 确认和支付状态
    confirmed = Column(Boolean, default=False)
    paid = Column(Boolean, default=False)
    paid_amount = Column(Float, default=0.0)
    paid_at = Column(DateTime(timezone=True))
    
    def to_dict(self):
        """转换为字典"""
        return {
            "userId": self.user_id,
            "userName": self.user_name,
            "avatar": self.avatar,
            "position": self.position,
            "confirmed": self.confirmed,
            "paid": self.paid,
            "paidAmount": self.paid_amount,
            "paidAt": self.paid_at.isoformat() if self.paid_at else None
        }

